package com.sangmo.fts.api.storage;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.process.storage.interfaces.UpdateDocumentRequest;
import com.sangmo.fts.process.storage.service.CsfContractDocumentService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@TrainingApi
@Controller
@RequestMapping(value = "/csfContract/v1")
@Access("csfContract")
public class CsfContractDocumentApi {

    @Autowired
    private CsfContractDocumentService csfContractDocumentService;

    @Access(":buyerUpload")
    @PostMapping({"/{trainingId}/{id}/buyer/document/upload"})
    @ResponseBody
    public ApiResponse<FileUpload> createBuyerDoc(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid UpdateDocumentRequest request) {
        return ApiResponse.make(csfContractDocumentService.createBuyerConfirmDoc(id, request.getFileUploadId()));
    }

    @Access(":sellerUpload")
    @PostMapping({"/{trainingId}/{id}/seller/document/upload"})
    @ResponseBody
    public ApiResponse<FileUpload> createSellerDoc(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid UpdateDocumentRequest request) {
        return ApiResponse.make(csfContractDocumentService.createSellerConfirmDoc(id, request.getFileUploadId()));
    }

}
